var mpops = new Array();

function MedalPopup(msg){
    this.msg = msg;
    this.stime = null;
    this.duration = 40; //seconds
}

function drawMedalPopup(){
    if(mpops.length > 0){
        if(mpops[0].stime){
            if((new Date().getTime() - mpops[0].stime.getTime()) > mpops[0].duration * 60){
                mmesg.style.visibility = "hidden";
                mpops.shift();
            }
        }else{
            mpops[0].stime = new Date();
            mmesg.innerHTML = "<b>ACHIEVEMENT UNLOCKED </b><br> " + mpops[0].msg; 
            mmesg.style.visibility = "visible";
        }
    }
}


function Medal(title, txt, param){
    this.title = title;
    this.txt = txt;
    this.param = param;
    this.isDone = false;
}

function MedalsTracker(){
    this.medals = new Array();
    this.medals.push(new Medal("Ready to Rock", "Complete the tutorial", 2)); //check against wave;
    this.medals.push(new Medal("Dedicated Star", "Complete ten waves", 11)); //check against wave;
    this.medals.push(new Medal("Jumping Jack", "Avoid getting crushed ten times", 10)); //check against bounce variable of map
    this.medals.push(new Medal("Star Dust", "Achieve two hundred score", 200)); //check against hero.pickup 
    this.medals.push(new Medal("Touch Me Not", "Complete five waves without enemy collision", 6)); //check against hero.tmn

    this.showMedal = showMedal;
    this.update =  function(){
        if(wave == this.medals[0].param){
            this.showMedal(0);
        }
        if(wave == this.medals[1].param){
            this.showMedal(1);
        }
        if(hero.crushmiss >= this.medals[2].param){
            this.showMedal(2);
        }
        if(hero.pickups >= this.medals[3].param){
            this.showMedal(3);
        }
        if(hero.tmn >= this.medals[4].param){
            this.showMedal(4);
        }
        drawMedalPopup();
    }
    this.drawMedals = drawMedals;

    this.getMedals = function() {
        var str = "";
        for(var i in this.medals){
            if(this.medals[i].isDone){
                str = str + this.medals[i].title + ":  " + this.medals[i].txt + ", " ;
            }
        }
        return str;
    }
}

function drawMedals(){
    var count = 0;
    medallist.innerHTML = "<center><h4>ACHIEVEMENTS</h4></center>";
    for(var i in this.medals){
        if(this.medals[i].isDone){
            medallist.innerHTML += "<li><font color=#000000><b>"+this.medals[i].title + ":</b>" + this.medals[i].txt + "</font></li>";
        }else{
            medallist.innerHTML += "<li><font color=#cccccc><b>"+this.medals[i].title + ":</b>" + this.medals[i].txt + "</font></li>";
            count++;
        }        
    }
    if(game_over == NO && count == 0){
        game_over = WON;
    }
//    medallist.innerHTML += "</ul>";
    medallist.innerHTML += "</table>";
}

function showMedal(i){
    if(!this.medals[i].isDone){
        mpops.push(new MedalPopup(this.medals[i].title));
        this.medals[i].isDone = true;
    }
    this.drawMedals();
}
